﻿using System;
using System.IO;
using System.Text;
using _5E.Infrastructure.Logging;
using log4net;
using log4net.Config;
using StoneAge.Infrastructure.Configuration;

namespace StoneAge.Infrastructure.Logging
{
    public class Log4NetAdapter : ILogger
    {
        private ILog m_log = null;

        public Log4NetAdapter()
        {
            FileInfo file = new FileInfo(ApplicationSettingsFactory.ApplicationSettings.LogConfigFilePath);
            XmlConfigurator.ConfigureAndWatch(file);
            m_log = LogManager.GetLogger(ApplicationSettingsFactory.ApplicationSettings.LogName);
        }

        public void Log(string message)
        {
            m_log.Info(message);
        }

        public void Log(Exception ex)
        {
            if (null != ex)
            {
                if (null == ex.InnerException)
                {
                    StringBuilder log = new StringBuilder()
                        .Append("Message:").AppendLine()
                        .AppendFormat("    {0}", ex.Message).AppendLine()
                        .Append("Source:").AppendLine()
                        .AppendFormat("    {0}", ex.Source).AppendLine()
                        .Append("StackTrace:").AppendLine()
                        .Append(ex.StackTrace);
                    Log(log.ToString());
                }
                else
                {
                    Log(ex.InnerException);
                }
            }
        }
    }    
}
